package com.ysd.resourceusestatistics.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.ysd.resourceusestatistics.dao.IMemberDao;
import com.ysd.resourceusestatistics.dto.MemberDTO;
import com.ysd.resourceusestatistics.model.Member;
import com.ysd.resourceusestatistics.service.IMemberService;
import com.ysd.resourceusestatistics.util.ExportExcelUtil;
import com.ysd.resourceusestatistics.util.POIUtil;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * MemberController
 */
@ApiOperation("会员列表")
@RestController
@RequestMapping("/member")
public class MemberController {

    @Resource
    private IMemberService memberService;

    @Resource
    private IMemberDao memberDao;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/findPage")
    public Map<String,Object> findPage(@RequestBody MemberDTO dto) {
Map<String,Object> map = new HashMap<String,Object>();
Page<Member> page = memberService.findPage(dto);
map.put("code",0);
map.put("data",page.getContent());
        return map;

    }

    @ApiOperation(value = "保存会员信息", notes = "添加或修改")
    @PostMapping("/savaMember")
    public Member savaMember(@RequestBody Member m) {

        return memberService.saveMember(m);

    }

    @ApiOperation(value = "删除会员", notes = "删除会员")
    @ApiImplicitParam(name = "id", value = "主键", required = true, paramType = "path")
    @PostMapping("/del")
    public void del(@RequestParam(value = "id",required = false) Integer id) {

        memberService.del(id);

    }

    @ApiOperation(value = "批量删除", notes = "批量删除")
   @PostMapping("/delBatch")
    public void delBatch(@RequestBody List<Member> list) {

         memberService.delBatch(list);

    }

    @ApiOperation(value = "批量添加", notes = "批量添加")
    @PostMapping(value = "/addbatch")
    public int batchAdd(String path) {
        // path参数是前台页面上传时的文件名，这里使用本地文件模拟下
        path = "C:\\Users\\hasee\\Desktop\\会员登记表.xlsx";
        List<Member> list = new ArrayList<Member>();
        try {
            list = POIUtil.importMember(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return memberService.addAll(list);
    }

    // 批量导出
    @ApiOperation(value = "导出", notes = "导出")
    @RequestMapping("/export")
    public void export() {
        List<Member> list = memberDao.findAll();

        ExportExcelUtil.exportExcelFile(list, "会员信息", "会员表", Member.class, "会员信息登记表.xls");

    }

}